Qt.include("db.js");

var dbManager;

// call initialize function
init();


function init() {
    dbManager = new DbManager();
}


// Generate dynamic Lists data model
function generateListItemsModel(model) {
    var obj = new Object();

    var res = dbManager.getLists();
    if (res == "error") {
        return;
    }

    model.clear();
    for(var i = 0; i < res.rows.length; i++) {
        obj.itemIndex =  i;
        obj.itemId =  res.rows.item(i).list_id;
        obj.itemName =  res.rows.item(i).list_name;
        //obj.itemLocation =  res.rows.item(i).list_location;
        //obj.itemIcon =  res.rows.item(i).icon_path;
        obj.itemPrice =  "100";
        obj.itemCurrency =  res.rows.item(i).currency;
        obj.itemTimestamp =  res.rows.item(i).list_timestamp;

        model.insert(i, obj);
        obj = {};
    }
    //model.sync();
    return;
}


// Generate dynamic Items data model
function generateItemsModel(list_id, model) {
    var obj = new Object();

    var res = dbManager.getItemsInList(list_id);

    if (res == "error") {
        return;
    }
    model.clear();
    for(var i = 0; i < res.rows.length; i++) {
        obj.productId = res.rows.item(i).item_id;
        obj.productListId = res.rows.item(i).item_list_id;
        obj.productName = res.rows.item(i).item_name;
        obj.productQuantity = res.rows.item(i).item_quantity;
        obj.productMU = res.rows.item(i).item_measuring_unit;
        obj.productCurrency = res.rows.item(i).item_currency;
        obj.productTimestamp = res.rows.item(i).item_timestamp;
        obj.productState = res.rows.item(i).item_checked;
        obj.productPrice = res.rows.item(i).item_price;
        obj.productIndex = i;

        model.insert(i, obj);
        obj = {};
    }
    console.log("Model done")

    return;
}


// Generate Settings data model
function generateSettingsModel() {
    var qmlObjectString = 'import QtQuick 1.0;';
    qmlObjectString += 'ListModel {';

    var res = dbManager.getSettingsData();

    if (res == "error") {
        return;
    }

    for(var i = 0; i < res.rows.length; i++) {
        qmlObjectString += 'ListElement {';
        qmlObjectString += 'itemId: "'       + res.rows.item(i).setting_id + '" ; ';
        qmlObjectString += 'itemMetric: "'   + res.rows.item(i).metric + '" ; ';
        qmlObjectString += 'itemCurrency: "' + res.rows.item(i).currency + '"; ';
        qmlObjectString += '}';
    }
    qmlObjectString += '}';

    return Qt.createQmlObject(qmlObjectString, parentId, "dynamicSettings");
}


// Generate Icons data model
function generateIconsModel() {
    var qmlObjectString = 'import QtQuick 1.0;';
    qmlObjectString += 'ListModel {';

    var res = dbManager.getIconsData();

    if (res == "error") {
        return;
    }

    for(var i = 0; i < res.rows.length; i++) {
        qmlObjectString += 'ListElement {';
        qmlObjectString += 'itemId: "'       + res.rows.item(i).icon_id + '" ; ';
        qmlObjectString += 'itemPath: "'   + res.rows.item(i).icon_path + '" ; ';
        qmlObjectString += '}';
    }
    qmlObjectString += '}';

    return Qt.createQmlObject(qmlObjectString, parentId, "dynamicIconsList");
}

//Delete item from list
function deleteItemFromList(item_id, model, list_id) {
    var res = dbManager.removeItemFromList(item_id);
    generateItemsModel(list_id, model);
    if (res == "error") return;
}

//Mark item in list as checked/unchecked - checked = {0,1}
function checkItem(checked, item_id) {
    var res = dbManager.checkItem(checked, item_id);
    if (res == "error") return;
}

//Delete list
function deleteList(list_id, model) {
    var res = dbManager.removeList(list_id);
    generateListItemsModel(model)
    if (res == "error") return;
}

//Insert/update list
function setList(name, model, loc, icon, listID) {
    var res = dbManager.setListData(name, loc, icon, listID);
    generateListItemsModel(model)
    if (res == "error") return;
}

//Insert new product
function setProduct(name, model, ammount, price, mu, listId) {
    var res = dbManager.setItemData(name, ammount, mu, 0.00, 0, listId);
    generateItemsModel(listId, model);
    if (res == "error") return;
}

//Load the saved setings data
function loadSettingsData() {
    var res = dbManager.getSettingsData();
    if (res == "error") return;

    if (res.rows.length < 1){
        console.log("No settings in database!");
        return;
    }
    mainContainer.var_metric = res.rows.item(0).metric;
    mainContainer.var_currency = res.rows.item(0).currency;
    return;
}

//Save setting to the database
function saveSettings(metric, currency, model, listId) {
    var res = dbManager.setSettingsData(metric, currency);
    if (res == "error") return;
}

//Load the saved setings data
function loadItemData(item_id, item_list_id) {
    mainContainer.item_id = 0;
    mainContainer.item_list_id = res.rows.item(0).item_list_id;
    var res = dbManager.getItemData(item_id);
    if (res == "error") return;

    if (res.rows.length < 1){
        console.log("No item data in database!");
        return;
    }else{
        mainContainer.item_id = res.rows.item(0).item_id;
        mainContainer.item_list_id = res.rows.item(0).item_list_id;
        mainContainer.item_name = res.rows.item(0).item_name;
        mainContainer.item_quantity = res.rows.item(0).item_quantity;
        mainContainer.item_measuring_unit = res.rows.item(0).item_measuring_unit;
        mainContainer.item_price= res.rows.item(0).item_price;
        mainContainer.item_checked = res.rows.item(0).item_checked;
    }
    return;
}
